const Task = require('../models/task')
const response = require('../utils/response')
const loop = require('../utils/loop.js')

// 搜索
exports.search = async (req, res, next) => {
    try {
        const userId = req.query.userId
        const taskname = req.query.taskname
        var task = await Task.find({
                "$and": [{
                        "taskname": {
                            "$regex": new RegExp(taskname)
                        }
                    },
                    {
                        "$or": [{
                                "worker": userId
                            },
                            {
                                "creator": userId
                            }
                        ]
                    }
                ]
            })
            .populate('worker', ['name'])
            .populate('creator', ['name'])
            .sort({
                _id: -1
            })
            .lean()
        await loop.deanTask(task)
        response(res, 200, '成功', task)
    } catch (err) {
        next(err)
    }
}